﻿namespace WMS6.Web.GlobalControl
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;
    using WMS6.Web.UserControls;

    public class WarehouseTransferList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNewSearch;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected HtmlForm F13511;
        protected GridView gvIntransit;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblAllocateRule;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2IntransitList(IntransitQueryEntity intransitQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(intransitQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = OutboundServiceClientProxy.GetInstance().GetIntransitInfoByQueryEntity(intransitQueryEntity);
            base.BindData2GridView(this.gvIntransit, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            IntransitQueryEntity intransitQuery = this.GetIntransitQueryEntity();
            intransitQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = intransitQuery.CurrentPage;
            intransitQuery.CurrentPage = "0";
            this.gvIntransit.Columns[1].Visible = false;
            this.gvIntransit.AllowSorting = false;
            this.BindData2IntransitList(intransitQuery);
            this.gvIntransit.ShowFooter = false;
            intransitQuery.IsGetAll = false;
            intransitQuery.CurrentPage = currentPageIndex;
            this.SetIntransitQueryEntity(intransitQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("Intransit.xls", this.gvIntransit);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            IntransitQueryEntity intransitQuery = this.GetIntransitQueryEntity();
            intransitQuery.CurrentPage = "0";
            this.BindData2IntransitList(intransitQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            IntransitQueryEntity intransitQuery = this.GetIntransitQueryEntity();
            intransitQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2IntransitList(intransitQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            IntransitQueryEntity intransitQuery = this.GetIntransitQueryEntity();
            intransitQuery.IntransitID = "";
            intransitQuery.SrcWhDbID = "";
            intransitQuery.DestWhDbID = "";
            intransitQuery.OwnerID = "";
            intransitQuery.OrderID = "";
            intransitQuery.ReceiptID = "";
            intransitQuery.OrderDate = "";
            intransitQuery.Status = "";
            intransitQuery.IsActive = "";
            this.BindData2IntransitList(intransitQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            IntransitQueryEntity intransitQuery = this.GetIntransitQueryEntity();
            if (int.Parse(intransitQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                intransitQuery.CurrentPage = (int.Parse(intransitQuery.CurrentPage) + 1).ToString();
                this.BindData2IntransitList(intransitQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            IntransitQueryEntity intransitQuery = this.GetIntransitQueryEntity();
            if (int.Parse(intransitQuery.CurrentPage) > 0)
            {
                intransitQuery.CurrentPage = (int.Parse(intransitQuery.CurrentPage) - 1).ToString();
                this.BindData2IntransitList(intransitQuery);
            }
        }

        private IntransitQueryEntity GetIntransitQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as IntransitQueryEntity);
            }
            return (base.PageQueryEntitySession as IntransitQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                IntransitQueryEntity intransitQuery = this.GetIntransitQueryEntity();
                intransitQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2IntransitList(intransitQuery);
            }
        }

        protected void gvIntransit_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvIntransit_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                IntransitQueryEntity intransitQuery = this.GetIntransitQueryEntity();
                intransitQuery.SortField = e.SortExpression;
                if (intransitQuery.SortDirection == "ASC")
                {
                    intransitQuery.SortDirection = "DESC";
                }
                else if (intransitQuery.SortDirection == "DESC")
                {
                    intransitQuery.SortDirection = "ASC";
                }
                this.BindData2IntransitList(intransitQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            IntransitQueryEntity intransitQuery = new IntransitQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    intransitQuery.IntransitID = this.PreviousPage.IntransitID;
                    intransitQuery.SrcWhDbID = this.PreviousPage.SrcWhDBID;
                    intransitQuery.DestWhDbID = this.PreviousPage.DestWhDBID;
                    intransitQuery.OwnerID = this.PreviousPage.OwnerID;
                    intransitQuery.OrderID = this.PreviousPage.OrderID;
                    intransitQuery.ReceiptID = this.PreviousPage.ReceiptID;
                    intransitQuery.OrderDate = this.PreviousPage.OrderDate;
                    intransitQuery.Status = this.PreviousPage.Status;
                    intransitQuery.IsActive = this.PreviousPage.IsActive;
                }
                else
                {
                    intransitQuery.IntransitID = this.PreviousPage.IntransitID;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            intransitQuery.IsGetAll = false;
            intransitQuery.CurrentPage = "0";
            intransitQuery.PageSize = this.gvIntransit.PageSize.ToString();
            intransitQuery.SortField = "INTRANSIT_ID";
            intransitQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvIntransit.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetIntransitQueryEntity(intransitQuery);
            this.BindData2IntransitList(intransitQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetIntransitQueryEntity(IntransitQueryEntity intransitQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = intransitQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = intransitQueryEntity;
            }
        }

        public WarehouseTransferQuery PreviousPage
        {
            get
            {
                return (WarehouseTransferQuery) base.PreviousPage;
            }
        }
    }
}

